home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 2
/
Nebula Two.iso
/
SourceCode
/
MiscKit1.7.1
/
MiscKit
/
Examples
/
getset
/
getset
next >
Wrap
Text File
|
1994-12-10
|
11KB
|
406 lines
#!/usr/local/bin/perl
# getset
#
# Copyright (c) 1994 by M. Onyschuk and Associates Inc.
# All Rights Reserved.
#
# HISTORY:
# Thu Mar 17 17:49:30 EST 1994 (MO)
# Starting point.
#
# Fri Mar 18 10:52:34 EST 1994
# Cleaned up code, though a PERL afficionado might argue!
#
# Mon Mar 21 14:25:23 EST 1994 (MO)
# Added class name support
#
# Tue Mar 22 09:52:40 EST 1994 (MO)
# Fixed character string set method to test whether aValue
# is NULL before calling NXCopyStringBufferFromZone.
#
# DESCRIPTION:
# Generates an Objective C persistent class from a list of attribute
# names and types of format:
#
# class-name superclass-name
# attribute-name-1 type-character-1
# attribute-name-2 type-character-2
# ...
#
# type-character may be one of:
#
# s = (const char *)
# i = (int)
# l = (long int)
# f = (float)
# d = (double)
# o = (id)
#
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
# process any FOO=bar switches
sub toupper {
# I know this is probably stupid, but I really couldn't PERL
# my way out of a wet paper bag! This was originally an AWK script -
# but I found I knew even less about AWK than PERL!
if ($_[0] eq 'a') { return "A"; }
if ($_[0] eq 'b') { return "B"; }
if ($_[0] eq 'c') { return "C"; }
if ($_[0] eq 'd') { return "D"; }
if ($_[0] eq 'e') { return "E"; }
if ($_[0] eq 'f') { return "F"; }
if ($_[0] eq 'g') { return "G"; }
if ($_[0] eq 'h') { return "H"; }
if ($_[0] eq 'i') { return "I"; }
if ($_[0] eq 'j') { return "J"; }
if ($_[0] eq 'k') { return "K"; }
if ($_[0] eq 'l') { return "L"; }
if ($_[0] eq 'm') { return "M"; }
if ($_[0] eq 'n') { return "N"; }
if ($_[0] eq 'o') { return "O"; }
if ($_[0] eq 'p') { return "P"; }
if ($_[0] eq 'q') { return "Q"; }
if ($_[0] eq 'r') { return "R"; }
if ($_[0] eq 's') { return "S"; }
if ($_[0] eq 't') { return "T"; }
if ($_[0] eq 'u') { return "U"; }
if ($_[0] eq 'v') { return "V"; }
if ($_[0] eq 'w') { return "W"; }
if ($_[0] eq 'x') { return "X"; }
if ($_[0] eq 'y') { return "Y"; }
if ($_[0] eq 'z') { return "Z"; }
return $_[0];
}
open(Ivars, ">/tmp/getset.Ivars");
open(Prototypes, ">/tmp/getset.Prototypes");
open(Methods, ">/tmp/getset.Methods");
open(Free, ">/tmp/getset.Free");
open(Read, ">/tmp/getset.Read");
open(Write, ">/tmp/getset.Write");
open(Copy,">/tmp/getset.Copy");
while (<>) {
($Attribute, $Type) = split(' ', $_, 9999);
$setAttribute = sprintf("set%s%s", do toupper(substr($Attribute, 0, 1)), substr($Attribute, 1));
if ($ClassName eq '') {
$ClassName = $Attribute;
$SuperclassName = $Type;
}
if ($Type eq 's') {
# Writes the ivar
printf Ivars " char *$Attribute;\n";
# Writes the prototype
printf Prototypes "- (const char *)$Attribute;\n";
printf Prototypes "- $setAttribute:(const char *)aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- (const char *)$Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:(const char *)aValue\n";
printf Methods "{\n";
printf Methods " NXZone *z = [self zone];\n";
printf Methods " if ($Attribute) {\n";
printf Methods " NXZoneFree(z, $Attribute);\n";
printf Methods " }\n";
printf Methods " $Attribute = (aValue) ? NXCopyStringBufferFromZone(aValue, z) : NULL;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the free body
printf Free " if ($Attribute) {\n";
printf Free " NXZoneFree(z, $Attribute);\n";
printf Free " $Attribute = NULL;\n";
printf Free " }\n";
# Writes the read: body
printf Read " NXReadType(stream, \"*\", &$Attribute);\n";
# Writes the write: body
printf Write " NXWriteType(stream, \"*\", &$Attribute);\n";
# Writes the copyFromZone: body
printf Copy " if ($Attribute) {\n";
printf Copy " copy->$Attribute = NXCopyStringBufferFromZone($Attribute, z);\n";
printf Copy " }\n";
}
if ($Type eq 'i') {
# Writes the ivar
printf Ivars " int $Attribute;\n";
# Writes the prototype
printf Prototypes "- (int)$Attribute;\n";
printf Prototypes "- $setAttribute:(int)aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- (int)$Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:(int)aValue\n";
printf Methods "{\n";
printf Methods " $Attribute = aValue;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the read: body
printf Read " NXReadType(stream, \"i\", &$Attribute);\n";
# Writes the write: body
printf Write " NXWriteType(stream, \"i\", &$Attribute);\n";
}
if ($Type eq 'l') {
# Writes the ivar
printf Ivars " long $Attribute;\n";
# Writes the prototype
printf Prototypes "- (long)$Attribute;\n";
printf Prototypes "- $setAttribute:(long)aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- (long)$Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:(long)aValue\n";
printf Methods "{\n";
printf Methods " $Attribute = aValue;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the read: body
printf Read " NXReadType(stream, \"l\", &$Attribute);\n";
# Writes the write: body
printf Write " NXWriteType(stream, \"l\", &$Attribute);\n";
}
if ($Type eq 'f') {
# Writes the ivar
printf Ivars " float $Attribute;\n";
# Writes the prototype
printf Prototypes "- (float)$Attribute;\n";
printf Prototypes "- $setAttribute:(float)aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- (float)$Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:(float)aValue\n";
printf Methods "{\n";
printf Methods " $Attribute = aValue;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the read: body
printf Read " NXReadType(stream, \"f\", &$Attribute);\n";
# Writes the write: body
printf Write " NXWriteType(stream, \"f\", &$Attribute);\n";
}
if ($Type eq 'd') {
# Writes the ivar
printf Ivars " double $Attribute;\n";
# Writes the prototype
printf Prototypes "- (double)$Attribute;\n";
printf Prototypes "- $setAttribute:(double)aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- (double)$Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:(double)aValue\n";
printf Methods "{\n";
printf Methods " $Attribute = aValue;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the read: body
printf Read " NXReadType(stream, \"d\", &$Attribute);\n";
# Writes the write: body
printf Write " NXWriteType(stream, \"d\", &$Attribute);\n";
}
if ($Type eq 'o') {
# Writes the ivar
printf Ivars " id $Attribute;\n";
# Writes the prototype
printf Prototypes "- $Attribute;\n";
printf Prototypes "- $setAttribute:aValue;\n";
printf Prototypes "\n";
# Writes the method body
printf Methods "- $Attribute\n";
printf Methods "{\n";
printf Methods " return $Attribute;\n";
printf Methods "}\n\n";
printf Methods "- $setAttribute:aValue\n";
printf Methods "{\n";
printf Methods " [$Attribute free];\n";
printf Methods " $Attribute = [aValue copyFromZone:[self zone]];\n";
printf Methods " return self;\n";
printf Methods "}\n\n";
printf Methods "- %sNoCopy:aValue\n", $setAttribute;
printf Methods "{\n";
printf Methods " [$Attribute free];\n";
printf Methods " $Attribute = aValue;\n";
printf Methods " return self;\n";
printf Methods "}\n";
printf Methods "\n";
# Writes the free body
printf Free " if ($Attribute) {\n";
printf Free " $Attribute = [$Attribute free];\n";
printf Free " }\n";
# Writes the read: body
printf Read " $Attribute = NXReadObject(stream);\n";
# Writes the write: body
printf Write " NXWriteObject(stream, $Attribute);\n";
# Writes the copyFromZone: body
printf Copy " copy->$Attribute = [$Attribute copyFromZone:aZone];\n";
}
}
close(Ivars);
close(Prototypes);
close(Methods);
close(Free);
close(Read);
close(Write);
printf "// Class $ClassName generated using getset 1.1\n";
printf "// Copyright 1994 by M. Onyschuk and Associates Inc.\n";
printf "// All Rights Reserved.\n";
printf "\n";
printf "#import \"$SuperclassName.h\"\n";
printf "\n";
printf "@interface $ClassName:$SuperclassName\n";
printf "{\n";
open(Ivars, "</tmp/getset.Ivars");
while (<Ivars>) {
printf "%s",$_;
}
close(Ivars);
printf "}\n";
printf "\n";
printf "- free;\n";
printf "\n";
printf "- read:(NXTypedStream *)aStream;\n";
printf "- write:(NXTypedStream *)aStream;\n";
printf "\n";
printf "- copyFromZone:(NXZone *)aZone;\n";
printf "\n";
open(Prototypes, "</tmp/getset.Prototypes");
while (<Prototypes>) {
printf "%s",$_;
}
close(Prototypes);
printf "@end\n";
printf "\n";
printf "#import \"$ClassName.h\"";
printf "\n";
printf "#import <objc/hashtable.h>\n";
printf "\n";
printf "@implementation $ClassName\n";
printf "\n";
printf "- free\n";
printf "{\n";
printf " NXZone *z = [self zone];\n";
printf "\n";
open(Free, "</tmp/getset.Free");
while (<Free>) {
printf "%s",$_;
}
close(Free);
printf "\n";
printf " return [super free];\n";
printf "}\n";
printf "\n";
printf "- read:(NXTypedStream *)stream\n";
printf "{\n";
printf " [super read:stream];\n";
printf "\n";
open(Read, "</tmp/getset.Read");
while (<Read>) {
printf "%s",$_;
}
close(Read);
printf "\n";
printf " return self;\n";
printf "}\n";
printf "\n";
printf "- write:(NXTypedStream *)stream\n";
printf "{\n";
printf " [super write:stream];\n";
printf "\n";
open(Write, "</tmp/getset.Write");
while (<Write>) {
printf "%s",$_;
}
close(Write);
printf "\n";
printf " return self;\n";
printf "}\n";
printf "\n";
printf "- copyFromZone:(NXZone *)aZone\n";
printf "{\n";
printf " $ClassName *copy = [super copyFromZone:aZone];\n";
printf "\n";
open(Copy, "</tmp/getset.Copy");
while (<Copy>) {
printf "%s",$_;
}
close(Copy);
printf "\n";
printf " return copy;\n";
printf "}\n";
printf "\n";
open(Methods, "</tmp/getset.Methods");
while (<Methods>) {
printf "%s",$_;
}
close(Methods);
printf "@end\n";
printf "\n";